from django.db import models
from api.path import *
from django.db.models.signals import pre_delete  # 删除文件
from django.dispatch.dispatcher import receiver  # 删除文件

# Create your models here.

class SetMeal(models.Model):
    
    _id = models.BigAutoField(primary_key=True)
    title = models.CharField(max_length=50)
    price = models.FloatField(default=100)
    domainNameNumber = models.IntegerField(default=1)
    payMethodNumber = models.IntegerField(default=1)
    desc = models.TextField()

    class Meta:
        db_table = "setmeal"

class SetMealOrder(models.Model):

    _id = models.BigAutoField(primary_key=True)
    createTime = models.DateTimeField(auto_now_add=True)
    serviceProvider = models.ForeignKey("ServiceProvider", on_delete=models.CASCADE)
    setmeal = models.ForeignKey("SetMeal", on_delete=models.CASCADE)
    price = models.FloatField(default=0)

    class Meta:
        db_table = "setmeal_order"



class ServiceProvider(models.Model):
    
    _id = models.BigAutoField(primary_key=True)
    name = models.CharField(max_length=50)
    phone = models.CharField(max_length=11,unique=True)
    password = models.CharField(max_length=64)
    balance = models.FloatField(default=0)
    credit = models.IntegerField(default=0)

    class Meta:
        db_table = "service_provider" 

class Consumer(models.Model):

    _id = models.BigAutoField(primary_key=True)
    name = models.CharField(max_length=50,null=True)
    phone = models.CharField(max_length=11)
    password = models.CharField(max_length=64)
    court = models.OneToOneField("Court", on_delete=models.CASCADE,null=True)
    serviceProviders = models.ManyToManyField("ServiceProvider")#该消费者属于哪些服务商

    class Meta:
        db_table = "consumer"

class ShoppingAddress(models.Model):

    _id = models.BigAutoField(primary_key=True)
    name = models.CharField(max_length=50)
    phone = models.CharField(max_length=11)
    address = models.CharField(max_length=100)
    addressDetail = models.CharField(max_length=300)
    consumer = models.ForeignKey("Consumer", on_delete=models.DO_NOTHING,null=True)
    deleteStatus = models.BooleanField(default=False)#改地址是否已经删除

    class Meta:
        db_table = "shopping_address"


class AliPay(models.Model):

    _id = models.BigAutoField(primary_key=True)
    createTime = models.DateTimeField(auto_now_add=True)
    serviceProviderName = models.CharField( max_length=50,null=True)
    account = models.CharField( max_length=50,default="17744602018")
    appId = models.CharField( max_length=100)
    publicKey = models.TextField()
    privateKey = models.TextField()
    serviceProvider = models.ForeignKey("ServiceProvider", on_delete=models.CASCADE,null=True)

    class Meta:
        db_table = "alipay"

class XorPay(models.Model):

    _id = models.BigAutoField(primary_key=True)
    createTime = models.DateTimeField(auto_now_add=True)
    serviceProviderName = models.CharField( max_length=50)
    account = models.CharField( max_length=50)
    appId = models.CharField( max_length=100)
    privateKey = models.TextField()
    serviceProvider = models.ForeignKey("ServiceProvider", on_delete=models.CASCADE)

    class Meta:
        db_table = "xorpay"

#相当于一个订单了用户购买后指向该用户
#createTime为购买的时间
class DomainName(models.Model):

    _id = models.BigAutoField(primary_key=True)
    domainname = models.CharField( max_length=50)
    price = models.FloatField(default=0)
    desc = models.TextField()

    class Meta:
        db_table = "domainname"

class DomainNameOrder(models.Model):

    _id = models.BigAutoField(primary_key=True)
    createTime = models.DateTimeField(auto_now_add=True)
    serviceProvider = models.ForeignKey("ServiceProvider", on_delete=models.CASCADE)
    domainname = models.ForeignKey("DomainName",on_delete=models.CASCADE)

    class Meta:
        db_table = "domainname_order"


class HelpDocument(models.Model):

    _id = models.BigAutoField(primary_key=True)
    createTime = models.DateTimeField(auto_now_add=True)
    content = models.TextField()

    class Meta:
        db_table = "helpdocument"

class Goods(models.Model):

    _id = models.BigAutoField(primary_key=True)
    createTime = models.DateTimeField(auto_now_add=True)
    goodsName = models.CharField( max_length=50)
    sellPrice = models.FloatField(default=0)
    originPrice = models.FloatField(default=0)
    stock = models.IntegerField(default=0)#库存
    sellNumber = models.IntegerField(default=0)
    detail = models.TextField()
    image1 = models.ImageField( upload_to=goodsImagePath,null=True)
    image2 = models.ImageField( upload_to=goodsImagePath,null=True)
    image3 = models.ImageField( upload_to=goodsImagePath,null=True)
    image4 = models.ImageField( upload_to=goodsImagePath,null=True)
    image5 = models.ImageField( upload_to=goodsImagePath,null=True)

    def sellData(self):
        data = dict()
        data["goodsName"] = self.goodsName
        data["sellPrice"] = self.sellPrice
        data["originPrice"] = self.originPrice
        data["detail"] = self.detail

        return data 

    class Meta:
        db_table = "goods"

@receiver(pre_delete, sender=Goods)  # sender=你要删除或修改文件字段所在的类**
def goodsPicDelete(instance, **kwargs):  # 函数名随意
    if instance.image1:
        instance.image1.delete(False)  # file是保存文件或图片的字段名**
    if instance.image2:
        instance.image2.delete(False)  # file是保存文件或图片的字段名**
    if instance.image3:
        instance.image3.delete(False)  # file是保存文件或图片的字段名**
    if instance.image4:
        instance.image4.delete(False)  # file是保存文件或图片的字段名**
    if instance.image5:
        instance.image5.delete(False)  # file是保存文件或图片的字段名**


class H5Template(models.Model):

    _id = models.BigAutoField(primary_key=True)
    title = models.CharField(max_length=50,unique=True)
    price = models.FloatField(default=0)
    desc = models.TextField()

    class Meta:
        db_table = "h5template"

class H5TemplateOrder(models.Model):

    _id = models.BigAutoField(primary_key=True)
    createTime = models.DateTimeField(auto_now_add=True)
    serviceProvider = models.ForeignKey("ServiceProvider", on_delete=models.CASCADE)
    h5template = models.ForeignKey("H5Template",on_delete=models.CASCADE)

    class Meta:
        db_table = "h5template_order"

class Link(models.Model):

    _id = models.BigAutoField(primary_key=True)
    title = models.CharField(max_length=30)
    createTime = models.DateTimeField(auto_now_add=True)
    domainname = models.CharField( max_length=50)
    paymethod = models.CharField( max_length=50)#alipay xorpay wechat win
    payId = models.IntegerField(null=False)#根据paymethod指向支付的实体ID
    serviceProvider = models.ForeignKey("ServiceProvider", on_delete=models.CASCADE)
    goods = models.ForeignKey("Goods", on_delete=models.CASCADE)
    h5template = models.ForeignKey("H5Template", on_delete=models.CASCADE)
    linkSellNumber = models.IntegerField(default=0)

    class Meta:
        db_table = "link"

class ConsumerOrder(models.Model):

    _id = models.BigAutoField(primary_key=True)
    createTime = models.DateTimeField(auto_now_add=True)
    expressNumber = models.CharField(max_length=50,null=True)
    expressName = models.CharField(max_length=30,null=True)
    orderNumber = models.CharField(max_length=50)#订单号
    status = models.CharField(max_length=10)#订单的状态 下单 运送中 已收货 未发货
    payStatus = models.CharField(max_length=20)#支付状态 未支付 已支付 已退单 申请退单
    consumer = models.ForeignKey("Consumer", on_delete=models.CASCADE)
    link = models.ForeignKey("Link",on_delete=models.CASCADE,null=True)
    shoppingAddress = models.ForeignKey("ShoppingAddress", on_delete=models.CASCADE,null=True)
    distributionId = models.CharField(null=True, max_length=50)
    tradeNo = models.CharField( max_length=28,null=True)#支付宝订单号
    goods = models.ForeignKey("Goods",null=True, on_delete=models.CASCADE)

    class Meta:
        db_table = "consumer_order"

class Court(models.Model):

    _id = models.BigAutoField(primary_key=True)
    goods = models.ManyToManyField("Goods")

    class Meta:
        db_table = "court"

